﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;

namespace demo05
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region Biến toàn cục
        IWMPMedia item;
        IWMPMediaCollection mediacol;
        IWMPControls3 control;
        #endregion
        int countplay = 0; int countpause = 0; int countstop = 0;

        #region Lấy thông tin
        void getInfo(IWMPMedia media)
        {
            txtTitle.Text = getTitle(media);
            txtAlbum.Text = getAlbum(media);
            txtArtist.Text = getArtist(media);
            txtRating.Text = getRating(media);
            txtYear.Text = getYear(media);
            //textBox1.Text = getCover(media);
        }

        string getTitle(IWMPMedia media)
        {
            string title = "";
            title = media.getItemInfo("Title");
            if (string.IsNullOrEmpty(title))
            {
                title = media.name;
            }
            return title;
        }

        string getAlbum(IWMPMedia media)
        {
            string album = "";
            album = media.getItemInfo("Album");
            if (string.IsNullOrEmpty(album))
            {
                album = "Unknown Album";
            }
            return album;
        }

        string getArtist(IWMPMedia media)
        {
            string artist = "";
            artist = media.getItemInfo("Artist");
            if (string.IsNullOrEmpty(artist))
            {
                artist = "Unknown Artist";
            }
            return artist;
        }

        string getRating(IWMPMedia media)
        {
            string rating = media.getItemInfo("UserRating");
            if (string.IsNullOrEmpty(rating))
            {
                rating = "0";
            }
            return rating;
        }

        string getYear(IWMPMedia media)
        {
            string year = "";
            year = media.getItemInfo("ReleaseDateYear");
            if (string.IsNullOrEmpty(year))
            {
                year = "Unknown Year";
            }
            return year;
        }

        string getCover(IWMPMedia media)
        {
            string cover = "";
            cover = media.getItemInfo("WM/AlbumCoverURL");
            return cover;
        }
        
        #endregion

        #region Cập nhật thông tin
        void setInfo(IWMPMedia media)
        {
            string title = media.getItemInfo("Title");
            if (title != txtTitle.Text.Trim())
            {
                media.setItemInfo("Title", txtTitle.Text.Trim());
            }
            string album = media.getItemInfo("Album");
            if (album != txtAlbum.Text.Trim())
            {
                media.setItemInfo("Album", txtAlbum.Text.Trim());
            }
            string artist = media.getItemInfo("Artist");
            if (artist != txtArtist.Text.Trim())
            {
                media.setItemInfo("Artist", txtArtist.Text.Trim());
            }
            string rating = media.getItemInfo("UserRating");
            if (rating != txtRating.Text.Trim())
            {
                media.setItemInfo("UserRating", txtRating.Text.Trim());
            }
            string year = media.getItemInfo("ReleaseDateYear");
            if (year != txtYear.Text.Trim())
            {
                media.setItemInfo("ReleaseDateYear", txtYear.Text.Trim());
            }
        }
        #endregion

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.mute = true;
            mediacol = axWindowsMediaPlayer1.mediaCollection;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                item = axWindowsMediaPlayer1.newMedia(openFileDialog1.FileName);
                getInfo(item);
                textBox1.Text = openFileDialog1.FileName;
                axWindowsMediaPlayer1.currentMedia = item;
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
            control = (IWMPControls3)axWindowsMediaPlayer1.Ctlcontrols;
            timer1.Start();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            setInfo(item);
            MessageBox.Show("Cập nhật thành công.");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //if (axWindowsMediaPlayer1.playState != WMPPlayState.wmppsStopped)
            //{
            //    lblTime.Text = control.currentPositionString;
            //}
            lblTime.Text = control.currentPositionString;
            if (lblTime.Text == item.durationString)
            {
                timer1.Stop();
            }
        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            //if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsPaused || axWindowsMediaPlayer1.playState == WMPPlayState.wmppsStopped)
            //{
            //    MessageBox.Show(lblTime.Text);
            //}
            if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsPlaying)
            {
                countplay++;
                lblPlay.Text = countplay.ToString();
            }
            else
            {
                if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsPaused)
                {
                    countpause++;
                    lblPause.Text = countpause.ToString();
                } 
                else
                {
                    if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsStopped)
                    {
                        countstop++;
                        lblStop.Text = countstop.ToString();
                    }
                }
            }
        }

    }
}
